//
//  FRHotViewController.m
//  Live
//
//  Created by 徐芙蓉 on 2017/7/26.
//  Copyright © 2017年 Afra. All rights reserved.
//

#import "FRHotViewController.h"
#import "FRLiveHandler.h"
#import "FRLiveCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import "FRPlayerViewController.h"

static NSString *identifier = @"FRLiveCell";

@interface FRHotViewController ()
@property (nonatomic, strong) NSMutableArray *datalist;
@end

@implementation FRHotViewController

-(NSMutableArray *)datalist
{
    if (!_datalist) {
        _datalist = [NSMutableArray array];
    }
    return _datalist;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initUI];
    [self loadData];
    
}

-(void)initUI
{
    [self.tableView registerNib:[UINib nibWithNibName:@"FRLiveCell" bundle:nil] forCellReuseIdentifier:identifier];
}

-(void)loadData
{
    [FRLiveHandler executeGetHotLiveTaskWithSuccess:^(id obj) {
        NSLog(@"%@",obj);
        [self.datalist addObjectsFromArray:obj];
         [self.tableView reloadData];
    } failed:^(id obj) {
    
        NSLog(@"%@",obj);
        
    }];
   
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datalist.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FRLiveCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        
        cell = [[FRLiveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    NSLog(@"datalist = %@",self.datalist[indexPath.row]);
    cell.live = self.datalist[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70 + SCREEN_WIDTH;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    FRLive *live = self.datalist[indexPath.row];
    
    FRPlayerViewController *playerVC = [[FRPlayerViewController alloc] init];
    playerVC.live = live;
    
    [self.navigationController pushViewController:playerVC animated:YES];
    
    
    /* 系统自带的播放器播放不了直播的内容
     
    MPMoviePlayerViewController *movieVC = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:live.streamAddr]];
    [self presentViewController:movieVC animated:YES completion:nil];
     */
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
